<template>
  <section>
    <el-row style="margin: -3px 0px 0px 1px">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/system/dashboard' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>客户信息</el-breadcrumb-item>
      </el-breadcrumb>
    </el-row>

    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" style="float:left;margin: 18px 27px -3px">
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
        <el-form-item>
          <!--          <stock-query @SearchByName="listCustomer" ref="searchBar"/>-->
        </el-form-item>
      </el-form>
    </el-col>

    <el-card style="margin: 18px 2%;width: 95%">
      <el-table
        :data="customerList"
        stripe
        style="width: 100%"
        @selection-change="selectChange"
        :max-height="tableHeight">
        <!--Form-->
        <el-table-column prop="name" label="名称" width="100"/>
        <el-table-column prop="carNum" label="车牌号"/>
        <el-table-column prop="phone" label="联系方式"/>
        <el-table-column prop="address" label="回访地址"/>
        <el-table-column prop="consumptionTotal" label="总消费"/>
        <el-table-column prop="isVip" label="是否为Vip">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.isVip"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="(value) => commitStatusChange(value, scope.row)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="140" fixed="right">
          <template slot-scope="scope">
            <el-button
              @click="editStock(scope.row)"
              type="text"
              size="small">
              编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-col :span="24" class="toolbar">
        <el-col :span="24" class="toolbar">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :page-sizes="[5, 10, 20]"
            :page-size="size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </el-col>
      </el-col>
    </el-card>
    <add_-update_-dialog @reload="listCustomer" ref="CustomerAddAndUpdate"/>
  </section>
</template>

<script>


import Add_Update_Dialog from "./component/Add_Update_Dialog";

export default {
  name: "Customer",
  components: {Add_Update_Dialog},
  data() {
    return {
      customerList: [],
      dialogFormVisible: false,
      size: 5,
      total: 0,
      page: 1,
      url: {
        list: '/customer/listCustomer',
        changeVip: '/customer/changeIsVip'
      }
    }
  },

  mounted() {
    this.listCustomer();
  },

  computed: {
    tableHeight() {
      return window.innerHeight - 320
    }
  },

  methods: {
    listCustomer() {
      this.$axios.post(this.url.list, {
        page: this.page,
        size: this.size,
        // name: this.$refs.searchBar.name
      }).then(resp => {
        if (resp.status === 200 && resp) {
          this.customerList = resp.data.result;
          this.total = resp.data.total
        }
      })
    },
    handleAdd() {
      this.$refs.CustomerAddAndUpdate.add();
    },
    editStock(customer) {
      this.$refs.CustomerAddAndUpdate.edit(customer)
    },
    commitStatusChange(value, customer) {
      console.log(value)
      if (value) {
        if (customer.consumptionTotal < 5000) {
          this.$message({
            message: customer.name + '总消费未达标，不能转vip用户',
            type: "warning"
          })
          customer.isVip = false
        } else {
          this.$confirm(`确认将${customer.name}${customer.isVip ? '升级为Vip用户吗？' : '取消Vip身份'}`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: "warning"
          }).then(() => {
            customer.isVip = true
            this.$axios.post(this.url.changeVip, customer).then(resp => {
              if (resp.data.code === 200) {
                this.$message({
                  message: resp.data.result,
                  type: "success"
                })
                this.listCustomer()
              }
            })
          }).catch(() => {
            if (customer.isVip) { // 非vip时升级取消
              customer.isVip = false
              this.$message.info("取消升级")
            } else { // vip客户取消时
              customer.isVip = true
              this.$message.info("取消操作")
            }
          })
        }
      } else {
        this.$confirm(`确认将${customer.name}${customer.isVip ? '升级为Vip用户吗？' : '取消Vip身份'}`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: "warning"
        }).then(() => {
          customer.isVip = false
          this.$axios.post(this.url.changeVip, customer).then(resp => {
            if (resp.data.code === 200) {
              this.$message({
                message: resp.data.result,
                type: "success"
              })
              this.listCustomer()
            }
          })
        }).catch(() => {
          if (customer.isVip) { // 非vip时升级取消
            customer.isVip = false
            this.$message.info("取消升级")
          } else { // vip客户取消时
            customer.isVip = true
            this.$message.info("取消操作")
          }
        })
      }
    },
    selectChange(checked) {
      this.checked = checked
    },
    handleCurrentChange(page) {
      this.page = page
      this.listCustomer()
    },
    handleSizeChange(val) {
      this.size = val
      this.listCustomer()
    }
  }
}
</script>

<style scoped>

</style>
